package com.sec.android.app.myfiles.module.local.foldertree;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteException;
import android.net.Uri;

import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.module.local.file.LocalFileRecord;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.provider.PrivateMyFilesProvider;

import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.command.SimpleCommand;

public class FolderTreeOpenCmd extends SimpleCommand {

    @Override
    public void execute(INotification notification) {
        Object[] params = (Object[]) notification.getBody();

        Context context = (Context) params[0];
        FileRecord record = (FileRecord) params[1];
        Boolean bOpen = true;

        if (record instanceof FolderTreeRecord || record instanceof LocalFileRecord) {
            if (record instanceof FolderTreeRecord) {
                bOpen = !((FolderTreeRecord) record).isOpened();
                ((FolderTreeRecord) record).setOpened(bOpen);
            }

            Log.d(this, record.getFullPath() + " open is " + bOpen);

            if (bOpen) {
                openFolder(context, record);
            } else {
                closeFolder(context, record);
            }
        }
    }

    private void openFolder(Context context, FileRecord record) {
        if (record.getStorageType() == StorageType.Local || record.getStorageType() == StorageType.FolderTree) {
            FileRecord refreshRecord = record;
            if (record.getStorageType() != StorageType.Local) {
                refreshRecord = new LocalFileRecord(record.getFullPath());
            }

            _openFolder(context, refreshRecord);
        }
    }

    private void _openFolder(Context context, FileRecord record) {
        if (record != null) {
            PrivateMyFilesProvider.refreshDb(context, record);

            ContentValues values = new ContentValues();
            values.put(FolderTreeProviderImp.COL_OPEN_PATH_NAME, record.getFullPath());
            DbTableInfo tableInfo = FolderTreeDbTableInfo.getInstance();
            Uri uri = Uri.parse(tableInfo.getUri());
            context.getContentResolver().insert(uri, values);

            if (!record.isRoot()) {
                _openFolder(context, record.getParent());
            }
        }

    }

    private void closeFolder(Context context, FileRecord record) {
        if (record.getStorageType() == StorageType.FolderTree) {
            DbTableInfo tableInfo = FolderTreeDbTableInfo.getInstance();
            Uri uri = Uri.parse(tableInfo.getUri());

            String where = new StringBuilder().append(FolderTreeProviderImp.COL_OPEN_PATH_NAME).append(" =? OR ")
                    .append(FolderTreeProviderImp.COL_OPEN_PATH_NAME).append(" like ?").toString();

            String[] args = new String[]{
                    record.getFullPath(), record.getFullPath() + "/%"};

            try {
                context.getContentResolver().delete(uri, where, args);
            } catch (SQLiteException e) {
                Log.d(this, "closeFolder() ] " + e.getMessage());
            }
        }

    }
}
